 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=65 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Choosing the right Collection
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='ArrayList,HashMap,HashSet,Hashtable,LinkedList,Properties,sort,sorting,Stack,TreeMap,TreeSet,Vector,collection,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Choosing the right Collection</div>

<div class='main-body'>
 
<br>Here is a guide for selecting the proper implementation of a 
<tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Set.html">Set</a>,
<a href="http://java.sun.com/javase/6/docs/api/java/util/List.html">List</a></tt>, 
or <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html">Map</a></tt>. 
It was compiled for Java 1.4. Many additions 
have been made to the <a href="http://java.sun.com/javase/6/docs/technotes/guides/collections/index.html">Collections Framework</a> since then (notably the 
<tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Queue.html">Queue</a></tt> and 
<tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Deque.html">Deque</a></tt> 
interfaces, and various items in <tt>java.util.concurrent</tt>).
These later additions have been omitted here, since this briefer summary should suffice for most cases.

<p>The best general purpose or 'primary' implementations are likely <tt>ArrayList</tt>, <tt>LinkedHashMap</tt>, and
<tt>LinkedHashSet</tt>. They are marked below as " * ". Their overall performance is better, and you should use them
unless you need a special feature provided by another implementation. That
special feature is usually ordering or sorting.

<p>Here, "ordering" refers to the order of items returned by an <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Iterator.html">Iterator</a></tt>,
and "sorting" refers to sorting items according to <tt><a href="http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html">Comparable</a></tt>
or <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html">Comparator</a></tt>.
<br>&nbsp;
<table BORDER CELLSPACING=0 WIDTH="100%" >
<tr>
<td>Interface</td>

<td>HasDuplicates?</td>

<td ALIGN=CENTER COLSPAN="5">Implementations</td>

<td>Historical</td>
</tr>

<tr>
<td><tt>Set</tt></td>

<td>no</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/HashSet.html">HashSet</a></tt></td>

<td ALIGN=CENTER>...</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/LinkedHashSet.html">LinkedHashSet</a></tt>*</td>

<td ALIGN=CENTER>...</td>

<td ALIGN=CENTER><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/TreeSet.html">TreeSet</a></tt></td>

<td>
<center>...</center>
</td>
</tr>

<tr>
<td><tt>List</tt></td>

<td>yes</td>

<td ALIGN=CENTER>...</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html">ArrayList</a></tt>*</td>

<td ALIGN=CENTER>...</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/LinkedList.html">LinkedList</a></tt></td>

<td>
<center>...</center>
</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Vector.html">Vector</a>,
<a href="http://java.sun.com/javase/6/docs/api/java/util/Stack.html">Stack</a></tt></td>
</tr>

<tr>
<td><tt>Map</tt></td>

<td>no duplicate keys&nbsp;</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/HashMap.html">HashMap</a></tt></td>

<td ALIGN=CENTER>...</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/LinkedHashMap.html">LinkedHashMap</a></tt>*</td>

<td ALIGN=CENTER>...</td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html">TreeMap</a></tt></td>

<td><tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Hashtable.html">Hashtable</a>,
<a href="http://java.sun.com/javase/6/docs/api/java/util/Properties.html">Properties</a></tt></td>
</tr>
</table>

<p>Principal features of non-primary implementations :
<ul>
<li><tt>HashMap</tt> has slightly better performance than <tt>LinkedHashMap</tt>, but its iteration order is 
<em>undefined</em>
</li>

<li><tt>HashSet</tt> has slightly better performance than <tt>LinkedHashSet</tt>, but its iteration 
order is <em>undefined</em> </li>

<li>
<tt>TreeSet</tt> is ordered and sorted, but slow</li>

<li>
<tt>TreeMap</tt> is ordered and sorted, but slow</li>

<li>
<tt>LinkedList</tt> has fast adding to the start of the list, and fast
deletion from the interior via iteration</li>
</ul>

Iteration order for above implementations :
<ul>
<li><tt>HashSet</tt> - <em>undefined</em></li>
<li><tt>HashMap</tt> - <em>undefined</em></li>
<li><tt>LinkedHashSet</tt> - insertion order</li>
<li><tt>LinkedHashMap</tt> - insertion order of keys (by default), or 'access order'</li>
<li><tt>ArrayList</tt> - insertion order</li>
<li><tt>LinkedList</tt> - insertion order</li>
<li><tt>TreeSet</tt> - ascending order, according to <tt>Comparable</tt> / <tt>Comparator</tt></li>
<li><tt>TreeMap</tt> - ascending order of keys, according to <tt>Comparable</tt> / <tt>Comparator</tt></li>
</ul>

For <tt>LinkedHashSet</tt> and <tt>LinkedHashMap</tt>, the re-insertion
of an item does not affect insertion order.
<p>For <tt>LinkedHashMap</tt>, 'access order' is from the least recent access
to the most recent access. In this context, only calls to <tt>get</tt>,
<tt>put</tt>,
and <tt>putAll</tt> constitute an access, and only calls to these methods
affect access order.
<p>While being used in a <tt>Map</tt> or <tt>Set</tt>, these items must
not change state (hence, it is recommended that these items be immutable
objects):
<ul>
<li>
keys of a <tt>Map</tt></li>

<li>
items in a <tt>Set</tt></li>
</ul>
Sorting requires either that :
<ul>
<li>
the stored items implement <a href='http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html'><tt>Comparable</tt></a></li>

<li>
a <a href='http://java.sun.com/javase/6/docs/api/java/util/Comparator.html'><tt>Comparator</tt></a> 
for the stored objects be defined</li>
</ul>
To retain the order of a <tt>ResultSet</tt> as specified in an ORDER BY
clause, insert the records into a <tt>List</tt> or a <tt>LinkedHashMap</tt>.
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction830f-2.html?Id=9'>Use standard Collections</a> <br>
 
  
  <a href='TopicAction3eed-2.html?Id=10'>Implementing compareTo</a> <br>
 
  
  <a href='TopicActiond838-2.html?Id=29'>Immutable objects</a> <br>
 
  
  <a href='TopicAction55c9-2.html?Id=39'>Prefer Collections over older classes</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='65'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=65 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
